<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="com.soft.model.*"%>
<%@ page language="java" import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="../../css/index.css" />
<link rel="stylesheet" href="../../css/common.css" />
<script type="text/javascript" src="../../js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="../../js/common.js" /></script>
<script type="text/javascript" src="../../js/admin/ready.js" /></script>
<script type="application/javascript">
	
	$(document).ready(function() {
		
		$("#insert").click(function(event) {
			window.location.href="permission_add";
		});
		
		$(".update").click(function(event) {
			var id = $(this).attr("currId");
			window.location.href="permission_upd?id="+id;
		});
		
		$(".delete").click(function(event) {
			var id = $(this).attr("currId");
			var row =  $(this).parent().parent();
			console.log(event.currentTarget.name);
			showMessage("I", "您确定要删除这个记录吗？", function() {
				$.ajax({
					url:"permission_del",
					data:"delIds="+id,
					type:"post",
					success:function(data){
						if(data == "ok"){
							//row.remove();
							$("#search").submit();
						}
					}
				});
				//window.location.href="store_del?id="+id;
			});;
		});
		
		$(".pageNum").click(function(event){
			var currPage = $(this).text();
			$("#currPage").val(currPage);
			$("#search").submit();
		});
		
		$(".page-area").children(".item").click(function(event) {
			$(".page-area").children(".item").each(function(index, target) {
				$(this).removeClass("selected-item");
			});
			$(this).addClass("selected-item");
		});
	});
	
	var getDataByPage = function(pageInfo) {
		console.log(pageInfo);
	}

</script>
</head>
<body class="content-body">
	<div class="title-area bg-color-black font-normal font-white">乌托邦别墅预定管理平台>系统设置>权限管理</div>
	<div class="search-area bg-color-gray">
		<form id="search" action="permission_list">
		<input type="hidden" id="currPage" name="currPage" value="1">
		<input class="row-item bg-color-white search-input" type="text"
			id="p_name" name="p_name" placeholder="请输入权限名称" value="${p_name }"/> 
		<select class="row-item bg-color-white search-select" id="parentId" name="parentId">
			<option value="0">==父级权限==</option>
			<%	
				String p1IdStr =(String) request.getAttribute("parentId");
				int p1_id = p1IdStr == null ? 0 : Integer.parseInt(p1IdStr);
				List<Permission> list_p1 = (List<Permission>)request.getAttribute("list_p1");
				if(list_p1!=null && list_p1.size()>0){
					for(Permission p1 : list_p1){
						String selected = "";
						if(p1.getId() == p1_id){
							selected ="selected";
						}
			%>
			<option value="<%=p1.getId() %>" <%=selected %>><%=p1.getPermissionName() %></option>
			<%			
					}
				}
			%>
		</select> 
		<button class="button-get button-item" id="search">查询</button> 
		<span class="button-put button-item" id="insert">新增</span>
		</form>
	</div>

	<!-- permission list -->
	<div class="table bg-color-gray">
		<div class="row">
			<div class="col-1">
				<div class="grid-title">ID</div>
			</div>
			<div class="col-1">
				<div class="grid-title">权限名称</div>
			</div>
			<div class="col-2">
				<div class="grid-title">权限内容</div>
			</div>
			<div class="col-1">
				
				<div class="grid-title">父级权限</div>
			</div>
			<div class="col-3">
				<div class="grid-title">操作</div>
			</div>
		</div>
		<%
			Map<Integer, Permission> map_p1 = (Map<Integer, Permission>)request.getAttribute("map_p1");
			List<Permission> list_permission =(List<Permission>) request.getAttribute("list_permission");
			if(list_permission!=null && list_permission.size()>0){
				for(Permission permission : list_permission){
					pageContext.setAttribute("permission", permission);
		%>
		<div class="row row-border">
			<div class="col-1">
				<div class="grid-label">${permission.id }</div>
			</div>
			<div class="col-1">
				<div class="grid-label">${permission.permissionName }</div>
			</div>
			<div class="col-2">
				<div class="grid-label">${permission.permissionContext }</div>
			</div>
			<div class="col-1">
			<%
				String parentName = "";
				int parentId = permission.getParentId();
				if(map_p1 == null || parentId==0 || !map_p1.containsKey(parentId)){
					parentName = "-";
				}else{
					parentName = map_p1.get(parentId).getPermissionName();
				}
			%>
				<div class="grid-label"><%=parentName %></div>
			</div>
			<div class="col-3">
				<div class="update width-two grid-button button-put" currId="${permission.id }">编辑</div>
				<div class="delete width-two grid-button button-del" currId="${permission.id }">删除</div>
			</div>
		</div>
		<%
				}
			}
		%>
	</div>

	<!-- page_area -->
	<div class="page-area">
		<div option="front" class="option font-small font-white button-get"><<</div>
		<div class="page-list">
			<div class="page-c">
				<%
					int pageCount = (int) request.getAttribute("pageCount");
					String pageStr = (String) request.getAttribute("currPage");
					int currPage = pageStr == null ? 1 : Integer.parseInt(pageStr);
					for (int i = 1; i <= pageCount; i++) {
						String addClass = "";
						if (i == currPage) {
							addClass = "selected-item";
						}
				%>
					<div target-page="<%=(i-1)%5+1 %>" class="item font-small font-white button-get pageNum <%=addClass %>"><%=i %></div>
				<% }%>
			</div>
		</div>
		<div option="back" class="option font-small font-white button-get">>></div>
	</div>
	<span style="color: blue;font-size: 16px;">共${totalCount}条记录</span>
</body>
</html>